package com.woniu.woniuproject.controller.admin;

import com.woniu.woniuproject.entity.Account_log;
import com.woniu.woniuproject.entity.Undertaker;
import com.woniu.woniuproject.mapper.AccountMapper;
import com.woniu.woniuproject.mapper.Account_logMapper;
import com.woniu.woniuproject.mapper.UndertakerMapper;
import com.woniu.woniuproject.mapper.moneyCountMapper;
import com.woniu.woniuproject.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author EDZ
 * @create 2020/11/25
 * @since 1.0.0
 */
@RestController
@RequestMapping("withdraw")
public class WithdrawController {

    @Autowired
    UndertakerMapper undertakerMapper;

    @Autowired
    AccountMapper accountMapper;
    @Autowired
    moneyCountMapper moneyCountMapper;

    @Autowired
    Account_logMapper account_logMapper;

    @RequestMapping("queryall")
    public ResponseResult<List<Undertaker>> queryall(){
        List<Undertaker> undertakers = undertakerMapper.selectAll();
        return  new ResponseResult<>(undertakers);
    }

    @RequestMapping("agree")
    @Transactional
    public ResponseResult<Integer> agree(Undertaker undertaker){
        //用户的id
        Integer id = undertaker.getId();
        String undertaker1 = undertaker.getUndertaker();
        //用户提现的金额
        Double money=Double.parseDouble(undertaker1);
        //扣掉手续费后的金额
        double money2=money-undertaker.getCost();
        //改状态
        int y=undertakerMapper.updateStatus(undertaker.getTid());
        //1.用户总额  余额扣钱
        int i=accountMapper.updateByUserKey2(id,money);

        //2.公司余额增加   记录增加
        int o=moneyCountMapper.updateadd(undertaker.getCost());

        //生成记录表
        Double accountmoney=moneyCountMapper.selectmoney();
        Account_log account_log = new Account_log();
        account_log.setAuserid(id);
        account_log.setAmainaccountid(1);
        account_log.setAreceiveorpay("收款");
        account_log.setAtransferserialno(UUID.randomUUID().toString().substring(0,8));
        account_log.setAdate(new Date());
        account_log.setAtype("用户提现手续费");
        account_log.setAamount(undertaker.getCost());
        account_log.setAbeforetradingmoney(accountmoney-undertaker.getCost());
        account_log.setAaftertradingmoney(accountmoney);
        int k=account_logMapper.sysinsert(account_log);
        if(i>0&&k>0&&o>0&&y>0){
            return  new ResponseResult<>(1);
        }else{
            return  new ResponseResult<>(0);
        }

    }

}